package net.lab1024.smartadmin.module.business.body.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.lab1024.smartadmin.common.constant.ResponseCodeConst;
import net.lab1024.smartadmin.common.controller.BaseController;
import net.lab1024.smartadmin.common.domain.PageResultDTO;
import net.lab1024.smartadmin.common.domain.ResponseDTO;
import net.lab1024.smartadmin.common.domain.ValidateList;
import net.lab1024.smartadmin.module.business.body.domain.dto.BodyAddDTO;
import net.lab1024.smartadmin.module.business.body.domain.dto.BodyQueryDTO;
import net.lab1024.smartadmin.module.business.body.domain.dto.BodyUpdateDTO;
import net.lab1024.smartadmin.module.business.body.domain.entity.BodyEntity;
import net.lab1024.smartadmin.module.business.body.domain.vo.BodyExcelVO;
import net.lab1024.smartadmin.module.business.body.domain.vo.BodyVO;
import net.lab1024.smartadmin.module.business.body.service.BodyService;
import net.lab1024.smartadmin.util.SmartRequestTokenUtil;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * [ 车身 ]
 *
 * @author 何杺
 * @version 1.0
 * @company 汽车传动
 * @copyright (c)  汽车传动Inc. All rights reserved.
 * @date 2021-11-19 11:18:27
 * @since JDK1.8
 */
@RestController
@Api(tags = {"车身"})
public class BodyController extends BaseController {

    @Autowired
    private BodyService bodyService;

    @ApiOperation(value = "根据id获取数据",notes = "@author 何杺")
    @GetMapping("/body/getById/{id}")
    public ResponseDTO<BodyEntity> getById(@PathVariable Long id) {
        return ResponseDTO.succData(bodyService.getById(id));
    }

    @ApiOperation(value = "分页查询车身",notes = "@author 何杺")
    @PostMapping("/body/page/query")
    public ResponseDTO<PageResultDTO<BodyVO>> queryByPage(@RequestBody BodyQueryDTO queryDTO) {
        if(queryDTO.getCreateUser()!=null){
            queryDTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        }
        queryDTO.setIsIndexShow(1);
        queryDTO.setMyUserId(SmartRequestTokenUtil.getRequestUserId());
        return bodyService.queryByPage(queryDTO);
    }

    @ApiOperation(value = "添加车身",notes = "@author 何杺")
    @PostMapping("/body/add")
    public ResponseDTO<String> add(@RequestBody @Validated BodyAddDTO addTO){
        addTO.setCreateUser(SmartRequestTokenUtil.getRequestUserId());
        addTO.setCreateTime(new Date());
        if(bodyService.select(addTO.getSn(),null)){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return bodyService.add(addTO);
    }

    @ApiOperation(value="修改车身",notes = "@author 何杺")
    @PostMapping("/body/update")
    public ResponseDTO<String> update(@RequestBody @Validated BodyUpdateDTO updateDTO){
        if(bodyService.select(updateDTO.getSn(),updateDTO.getId())){
            return ResponseDTO.wrap(ResponseCodeConst.ERROR_SN_REPEAT);
        }
        return bodyService.update(updateDTO);
    }

    @ApiOperation(value="批量删除车身",notes = "@author 何杺")
    @PostMapping("/body/deleteByIds")
    public ResponseDTO<String> delete(@RequestBody @Validated ValidateList<Long> idList) {
        return bodyService.deleteByIds(idList);
    }

    @ApiOperation(value = "批量导出", notes = "@author 何杺")
    @PostMapping("/body/export/batch")
    public void batchExport(@RequestBody @Validated ValidateList<Long> idList, HttpServletResponse response) {
        //查询数据
        List<BodyExcelVO> bodyList = bodyService.queryBatchExportData(idList);
        //导出操作
        ExportParams ex = new ExportParams("车身", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, BodyExcelVO.class, bodyList);
        downloadExcel("车身", workbook, response);
    }

    @ApiOperation(value = "导出全部", notes = "@author 何杺")
    @PostMapping("/body/export/all")
    public void exportAll(@RequestBody @Validated BodyQueryDTO queryDTO, HttpServletResponse response) {
        //查询数据
        List<BodyExcelVO> bodyList = bodyService.queryAllExportData(queryDTO);
        //导出操作
        ExportParams ex = new ExportParams("车身", "Sheet1");
        Workbook workbook = ExcelExportUtil.exportExcel(ex, BodyExcelVO.class, bodyList);
        downloadExcel("车身", workbook, response);
    }

}
